import {createRouter, createWebHistory} from 'vue-router'
import BaseView from "@/views/BaseView.vue";
import request from "@/utils/AxiosUtils";
// import useStore from "@/store/UserStore"
import loginStore from "@/store/loginStore";
import pinia from "@/store/pinia";
import {ElMessage} from "element-plus";
import AdminView from "@/views/admin/AdminView.vue";
import Index from "@/components/admin/Index.vue";
import FrontIndex from "@/components/index/FrontIndex.vue";


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            component: BaseView,
            children: [
                {
                    path: "",
                    name: "index",
                    component: FrontIndex
                },
                {
                    path: "albums",
                    children: [
                        {
                            path: "",
                            name: "albums",
                            component: () => import("@/components/index/Albums.vue")
                        },
                        {
                            path: ":albumId",
                            props: true,
                            component: () => import("@/components/index/Picture.vue")
                        },
                    ]
                },

                {
                    path: "categories",
                    children: [
                        {
                            path: "",
                            name: "categories",
                            component: () => import("@/components/blog/Category.vue")
                        },
                        {
                            path: ":categoryId",
                            props: true,
                            component: () => import("@/components/blog/CategoryShow.vue")
                        },
                    ]
                },
                {
                    path: "tag",
                    children: [
                        {
                            path: "",
                            name: "frontTag",
                            component: () => import("@/components/blog/Tag.vue")
                        },
                        {
                            path: ":tagId",
                            props: true,
                            component: () => import("@/components/blog/TagShow.vue")
                        },
                    ]
                },
                {
                    path: "allBlogs",
                    children: [
                        {
                            path: "",
                            name: "allBlogs",
                            component: () => import("@/components/blog/AllBlogs.vue")
                        },
                        // {
                        //     path: ":searchText",
                        //     props: true,
                        //     component: () => import("@/components/blog/AllBlogs.vue")
                        // },
                    ]
                },

                {
                    path: "timeline",
                    name: "timeline",
                    component: () => import("@/components/blog/TimeLine.vue")
                },
                {
                    path: "friendLink",
                    name: "friendLink",
                    component: () => import("@/components/index/FriendLink.vue")
                },

                {
                    path: "article/:blogId",
                    props: true,
                    component: () => import("@/components/blog/Article.vue")
                },
                {
                    path: "about",
                    name: "about",
                    component: () => import("@/components/index/About.vue")
                },
                {
                    path: "userCenter",
                    name: "userCenter",
                    component: () => import("@/components/index/UserCenter.vue")
                },
                {
                    path: "star",
                    name: "star",
                    component: () => import("@/components/index/Star.vue")
                },
                {
                    path: "callback/:accountType",
                    name: "callback",
                    props: true,
                    component: () => import("@/components/login/callback.vue")
                },

            ]
        },
        {
            path: '/admin',
            component: AdminView,
            children: [
                {
                    path: "",
                    name: "admin",
                    component: Index
                },
                {
                    path: "editBlog/:id",
                    name: "editBlog",
                    props: true,
                    component: () => import("@/components/admin/EditBolg.vue")
                },
                {
                    path: "category",
                    name: "category",
                    component: () => import("@/components/admin/CategoryManage.vue")
                },
                {
                    path: "userManage",
                    name: "userManage",
                    component: () => import("@/components/admin/UserManage.vue")
                },
                {
                    path: "tag",
                    name: "tag",
                    component: () => import("@/components/admin/TagManage.vue")
                },
                {
                    path: "article",
                    name: "article",
                    component: () => import("@/components/admin/ArticleManage.vue")
                },
                {
                    path: "aphorism",
                    name: "aphorism",
                    component: () => import("@/components/admin/Aphorism.vue")
                },
                {
                    path: "album",
                    name: "album",
                    component: () => import("@/components/admin/Album.vue")
                },
                {
                    path: "picture/:albumId",
                    name: "picture",
                    props: true,
                    component: () => import("@/components/admin/Picture.vue")
                },
                {
                    path: "druid",
                    name: "druid",
                    component: () => import("@/components/admin/monitor/Druid.vue")
                },
                {
                    path: "swagger",
                    name: "swagger",
                    component: () => import("@/components/admin/monitor/Swagger.vue")
                },
            ]
        },
        {
            path: '/login',
            children: [
                {
                    path: '',
                    name: 'login',
                    component: () => import('@/views/login/LoginView.vue'),
                },
                {
                    path: 'tokenError',
                    name: 'tokenError',
                    component: () => import("@/components/error/TokenError.vue")
                },
            ]
        },
        {
            path: '/errorPage/:msg/:status',
            name: 'errorPage',
            component: () => import("@/components/error/ErrorPage.vue"),
            props: true
        },
        {
            path: '/register',
            name: 'register',
            // 懒加载
            component: () => import('@/views/login/RegisterView.vue')
        }
    ]
})

router.beforeEach(async (to, from, next) => {

    // 在路由安装完之后就会开始导航
    const store = loginStore(pinia)

    // 如果是去admin 页面
    if (to.path.startsWith("/admin")) {
        // 检查token是否合法
        let {data: result} = await request.get("/api/user/checkToken")
        // console.log(result)
        if (result) {
            // 访问一下admin接口，如果不是超级用户，后端会拦截去往后台的请求
            let {data: right} = await request.get("/api/admin/testRight")
            next()
        } else {
            ElMessage.error({showClose: true, message: "登录信息验证失败，请先登录！", duration: 1000})
            // 保存要访问的路由，登录成功后直接跳转
            store.setNextPath(to)
            router.push({name: "login"})
        }
    } else if (to.path.startsWith("/star")) {
        // 检查token是否合法
        let {data: result} = await request.get("/api/user/checkToken")
        if (result) {
            next()
        } else {
            ElMessage.error({showClose: true, message: "登录信息验证失败，请先登录！", duration: 1000})
            // 保存要访问的路由，登录成功后直接跳转
            store.setNextPath(to)
            router.push({name: "login"})
        }
    } else {
        next()
    }
})

export default router
